<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
</html>
<script>
    //使用闭包按指定字段排序
    let lesson = [
        {
            title : "媒体查询响应式布局",
            click : 89,
            price : 12
        },
        {
            title : 'FLEX弹性盒子模型',
            click : 45,
            price : 120
        },
        {
            title : 'GRID栅格系统',
            click : 19,
            price : 67
        },
        {
            title : "盒子模型讲解",
            click : 29,
            price : 300
        }
    ];

    // lesson.sort(function(a,b){
    //     return a.price < b.price ? 1 : -1;        
    // });
    // console.log(lesson)

    function order(field,type='asc'){
        return function(a,b){
            //子函数是可以使用父函数的变量的
            if(type == 'asc'){
                return a[field] > b[field] ? 1 : -1;
            }else{
                return a[field] < b[field] ? 1 : -1;
            }
        }
    }
    lesson.sort(order('price','asc'));
    console.log(lesson)


</script>